package com.dc.common;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dc.constat.StatusCodeConstants;
import com.dc.exceptions.BizException;
import com.dc.ro.PageRO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class ResponseResultVO<T> implements Serializable {

    private static final long serialVersionUID = -4601700206373064418L;
    /**
     * 状态码
     */
    private int state;

    /**
     * 状态信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public ResponseResultVO() {
    }

    public ResponseResultVO(int state, String msg, T data) {
        this.state = state;
        this.data = data;
        this.msg = msg;
    }

    public static ResponseResultVO resultList(IPage iPage, PageRO page) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", iPage.getRecords());
        map.put("count", (int) iPage.getTotal());
        map.put("pageSize", page.getPageSize());
        map.put("currentPage", page.getCurrentPage());
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, "成功", map);
    }

    public static ResponseResultVO resultPageList(List list, PageRO page) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotalCount());
        map.put("pageSize", page.getPageSize());
        map.put("currentPage", page.getCurrentPage());
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, "成功", map);
    }

    /**
     * 对返回值的封装
     *
     * @param code
     * @param msg
     * @param object
     * @return
     */
    public static ResponseResultVO result(int code, String msg, Object object) {
        return new ResponseResultVO(code, msg, getData(object));
    }

    /**
     * 成功
     *
     * @param msg
     * @param object
     * @return
     */
    public static ResponseResultVO successResult(String msg, Object object) {
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, msg, getData(object));
    }

    /**
     * 成功
     *
     * @param msg
     * @return
     */
    public static ResponseResultVO successResult(String msg) {
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, msg, getData(null));
    }

    /**
     * 成功
     *
     * @param object
     * @return
     */
    public static ResponseResultVO successResult(Object object) {
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, "成功", getData(object));
    }


    /**
     * 成功
     *
     * @param list
     * @return
     */
    public static ResponseResultVO successResult(List list) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return new ResponseResultVO(StatusCodeConstants.SUCCESS, "成功", map);
    }

    /**
     * 失败
     *
     * @param msg
     * @param object
     * @return
     */
    public static ResponseResultVO failResult(String msg, Object object) {
        return new ResponseResultVO(StatusCodeConstants.FAIL, msg, getData(object));
    }

    /**
     * 失败
     *
     * @param msg
     * @return
     */
    public static ResponseResultVO failNoLoginResult(String msg) {
        return new ResponseResultVO(StatusCodeConstants.NOLOGINFAIL, msg, getData(null));
    }

    /**
     * 失败
     *
     * @param msg
     * @return
     */
    public static ResponseResultVO failResult(String msg) {
        return new ResponseResultVO(StatusCodeConstants.FAIL, msg, getData(null));
    }


    private static Object getData(Object object) {
        if (object == null) {
            return new HashMap<>();
        } else {
            return object;
        }
    }

    public static ResponseResultVO insertResult(int i) {
        if (i > 0) {
            return ResponseResultVO.successResult("新增成功");
        } else {
            return ResponseResultVO.failResult("新增失败");
        }
    }

    public static ResponseResultVO updateResult(int i) {
        if (i > 0) {
            return ResponseResultVO.successResult("修改成功");
        } else {
            return ResponseResultVO.failResult("修改失败");
        }
    }

    public static ResponseResultVO deleteResult(int i) {
        if (i > 0) {
            return ResponseResultVO.successResult("删除成功");
        } else {
            return ResponseResultVO.successResult("删除成功");
        }
    }

    public static void checkParameterIsNotNull(String var, String title) {
        if (StringUtils.isBlank(var) || "null".equals(var)) {
            throw new BizException(StrUtil.isBlank(title) ? "参数不能为空" : title);
        }
    }


}
